/**
  ******************************************************************************
  * @file 	MT9D111_stm_driver.c 
  * @brief  functions to test all units being used in this project
  ******************************************************************************
  */
#include "unitTest.h"
#include "misc.h"
//#include "zbProtocol.h"

// global vars

__IO uint16_t Data_Buffer[bufferSize];	// buffer for dma
//__IO u8 Packet_Buffer[maxPackets*(packetSize+overhead)];
u32 pwmTicks = 0;						// not used 
unsigned long pa0Ticks = 0;				// ticks on exti on pa0
unsigned long CCP2Ticks = 0;			// ticks on exti on pc6
float CCP2FQ = 0;						// frequency of pc6 line
u8 packetsToSend = 0;					// number of packets to send all data in buffer
u32 botStatus = 0;						// current botStatus or error state
//u32	seconds = 0;						// current seconds since 1970, uptime if time not set ! 
//u32 msticks = 0;						// millisecond ticks for calculating seconds 	
__IO uint32_t TimingDelay;				// for delay timer
//extern u8 g_PktBuff[pktBuffSize];


/**
  * @brief runs a test on a unit,  
  *        or multiple units together
  *         
  * @note   
  * @param  unitnumber to test
  * @retval error code
  */
u8 testUnit(u8 unitNum)
{


return 0x00; // no err 
}


/**
  * @brief  this will initialize all systems in order  
  *        
  *         
  * @note   This is everything required for the robot to work ?
  *			
  *			
  * @param  none
  * @retval none
  */
void initSystems(void)
{
	//mygVars->status |= STAT_STARTUP; // set botStatus bit

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);

	// todo test everyting is working ?
	// test here

	// todo cut and put power to camera or other things
	// power init here

	// init camera clk at 6MHz
	initPWM(6000000);

	//USART1_Config(); // especial?

	// init usart3 debug output 
	USART3_Config();	 // CONFLICTS I2C replaced by i2c2

	// init ccp that drives dma1 on pclk signal from camera
	initCCP1();

	// int dma for camera data rx
	initDMA1();

	// init botStatus leds
	initLed();

	//mygVars->status &= ~STAT_STARTUP;	// startup over
	//mygVars->status |= STAT_STARTED;		// now started

}
/**
  * @brief This inits the pwm output on tim2, pin 1 on port A  
  *        
  *         
  * @note   this is the driver for the camera clock, should be 6+Mhz
  *			anything lower than 72mhz/0xFFFF would need prescalar
  *			so thats anything lower than 1098HZ
  * @param  none
  * @retval none
  */
void initPWM(float frequency)
{
	TIM_TimeBaseInitTypeDef  	TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  			TIM_OCInitStructure;
	GPIO_InitTypeDef  			GPIO_InitStructure;
	u16 orgfrequency			= frequency; 
	u16	period					= (u16)((SystemCoreClock/frequency) -1); //period = (SystemCoreClock / fq ) - 1 ... ex 11 = 6mhz
	u16 pulse					= (5*(period-1))/10; 	// 50%
	u16 prescalar				= 0;
	
	TIM_Cmd( TIM2, DISABLE ); 	// in case they are restarting

	// if they are using a frequency low enough to merit a prescalar
	
	while(frequency < 1098)
	{
		prescalar++;
		frequency += orgfrequency;
		period					= (u16)((SystemCoreClock/frequency) -1); //period = (SystemCoreClock / fq ) - 1 ... ex 11 = 6mhz
		pulse					= (5*(period-1))/10; 	// 50%
	}

	// turn on the clocks for tim1 and GPIOA
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );

	// pin 1 port A is TIM2_CH2
	// for output tim2 use af_pp, see page 145 of datasheet table 20
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	//GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE);

	// Configure TIM1 timebase, TIM1CLK=72MHz, TIM1 Freq = clk/(period+1) = 72/12 = 6MHz
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
	TIM_TimeBaseStructure.TIM_Period			= period;				// active auto reload register
	TIM_TimeBaseStructure.TIM_Prescaler			= prescalar; 			// set this to 2 and its half the speed ? 
	TIM_TimeBaseStructure.TIM_ClockDivision		= 0;
	TIM_TimeBaseStructure.TIM_CounterMode		= TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_RepetitionCounter	= 0;
	TIM_TimeBaseInit( TIM2, &TIM_TimeBaseStructure );
	
	// Configure pwm output
	TIM_OCInitStructure.TIM_OCMode				= TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OutputState			= TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OutputNState		= TIM_OutputNState_Disable;
	TIM_OCInitStructure.TIM_Pulse				= pulse;				// (5*(TIM_Period-1))/10 for 50% (/1000 for 5% etc)
	TIM_OCInitStructure.TIM_OCPolarity			= TIM_OCPolarity_Low;
	TIM_OC2Init( TIM2, &TIM_OCInitStructure );							// NOTE OC2 means channel 2
	
	//Enable the timer (starts generating pulses)
	TIM_Cmd(TIM2, ENABLE);

	//Enable the OC pwm output
	TIM_CtrlPWMOutputs(TIM2, ENABLE);
}

void initEXTI(void)
{
	GPIO_InitTypeDef  			GPIO_InitStructure;
	EXTI_InitTypeDef   			EXTI_InitStructure;
	NVIC_InitTypeDef   			NVIC_InitStructure;

	// turn on the clocks for GPIOA
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE );

	// pin 1 port A is TIM2_CH2
	// for output tim2 use af_pp, see page 145 of datasheet table 20
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// Connect EXTI0 Line to PA.00 pin 
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);

	// Configure EXTI0 line 
	EXTI_InitStructure.EXTI_Line = EXTI_Line0;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);

	/* Enable and set EXTI0 Interrupt to the lowest priority */
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}
/**
  * @brief  This function handles External line 0 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI0_IRQHandler(void)
{
	pa0Ticks++;
    // Clear the  EXTI line 0 pending bit
    EXTI_ClearITPendingBit(EXTI_Line0);
}

/**
  * @brief This inits the Input Capture on TIM1  
  *        
  *         
  * @note   this is the trigger for our dma on pxclk from camera
  * @param  none
  * @retval none
  */
void initCCP1(void)
{
	// all init structs 
	TIM_TimeBaseInitTypeDef		TIM_TimeBaseStructure; 
	TIM_ICInitTypeDef			TIM_ICInitStructure;
	GPIO_InitTypeDef			GPIO_InitStructure;
	NVIC_InitTypeDef			NVIC_InitStructure;

	TIM_Cmd(TIM1, DISABLE); 	// in case they are restarting

	// turn on the clocks for tim1 and GPIOA
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA, ENABLE);

	// pin 11 port A is TIM1_CH4
	// for input capture channel or External trigger timer input use In floating
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_11 ;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// setup base values for the tim1
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  
	TIM_TimeBaseStructure.TIM_Period 		= 0x00;				// how many hits before causing event aka "auto reload value", active auto reload         
	TIM_TimeBaseStructure.TIM_Prescaler 	= 0;					// prescaler can divide the counter clock frequency by any factor between 1 and 65536, disabled       
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;    		  		// idk what is the diff in this and prescaler, maybe one is for oc the other for ic
	TIM_TimeBaseStructure.TIM_CounterMode 	= TIM_CounterMode_Up;	// count up to "auto reload value" then throw event
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;				// this is how many times period expires before throwing event, not used here
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
	
	// setup specifics of the tim1 ic      
	TIM_ICInitStructure.TIM_Channel 	= TIM_Channel_4;  			// set channel 4, portA pin 11        
	TIM_ICInitStructure.TIM_ICPolarity 	= TIM_ICPolarity_Rising;	// triggers on rising edge
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;			// div1 is no prescaler
	TIM_ICInitStructure.TIM_ICFilter = 0; 							// not to sure what this is, but as 0 its disabled
	TIM_ICInit(TIM1, &TIM_ICInitStructure);

	//  
//	NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
	NVIC_Init(&NVIC_InitStructure);

	// enable interrupt 
	//TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

	// enable this timer
	TIM_Cmd(TIM1, ENABLE);

	// enable this to trigger dma, CC4 enables the tim to trigger dma from channel 4 only todoTest
	TIM_DMACmd(TIM1, TIM_DMA_CC4, ENABLE);
}

/**
  * @brief This inits the Input Capture on TIM5  
  *        
  *         
  * @note   this is used to find the fq of any line
  * @param  none
  * @retval none
  */
void initCCP2(void)
{
#ifndef STM32F10X_LD_VL
	// all init structs 
	TIM_TimeBaseInitTypeDef		TIM_TimeBaseStructure; 
	TIM_ICInitTypeDef			TIM_ICInitStructure;
	GPIO_InitTypeDef  			GPIO_InitStructure;
	NVIC_InitTypeDef 			NVIC_InitStructure;
	//float frequency				= 3000000; 
	//u16	period					= (u16)((SystemCoreClock/3000000) -1); //period = (SystemCoreClock / fq ) - 1 ... ex 11 = 6mhz
	//u16 pulse					= (5*(period-1))/10; 	// 50%

	TIM_Cmd(TIM8, DISABLE); 	// in case they are restarting

	// turn on the clocks for tim1 and GPIOC
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8 | RCC_APB2Periph_GPIOC, ENABLE);

	// 
	// for input capture channel or External trigger timer input use In floating
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_9 ;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	// setup base values for the tim5
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  
	TIM_TimeBaseStructure.TIM_Period 		= 0xFFFF;				// how many hits before causing event aka "auto reload value", active auto reload         
	TIM_TimeBaseStructure.TIM_Prescaler 	= 0;					// prescaler can divide the counter clock frequency by any factor between 1 and 65536, disabled       
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;    		  		// idk what is the diff in this and prescaler, maybe one is for oc the other for ic
	TIM_TimeBaseStructure.TIM_CounterMode 	= TIM_CounterMode_Up;	// count up to "auto reload value" then throw event
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;				// this is how many times period expires before throwing event, not used here
	TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
	
	// setup specifics of the tim1 ic      
	TIM_ICInitStructure.TIM_Channel 	= TIM_Channel_4;  			// set channel 4     
	TIM_ICInitStructure.TIM_ICPolarity 	= TIM_ICPolarity_Rising;	// triggers on rising edge
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8;			// div8 rases a capture event every 8 times the ccp is hit
	TIM_ICInitStructure.TIM_ICFilter = 0; 							// not to sure what this is, but as 0 its disabled
	TIM_ICInit(TIM8, &TIM_ICInitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
	NVIC_Init(&NVIC_InitStructure);
	// enable interrupt on capture compare
	TIM_ITConfig(TIM8, TIM_IT_CC4, ENABLE);


	// enable this timer
	TIM_Cmd(TIM8, ENABLE);
#endif

}

/**
  * @brief int handler on caputre event from tim8 
  *       
  *         
  * @note should capture 1/8 of the times reducing fq enough to handle
  * @param  none
  * @retval none
  */
void TIM8_CC_IRQHandler(void)
{
	//TIM_DMACmd(TIM1, TIM_DMA_CC4, DISABLE);
	//TIM_Cmd(TIM8, DISABLE);
	// clear the pending bit so we dont block 
	CCP2Ticks++;
	TIM_ClearITPendingBit(TIM8, TIM_IT_CC4);
	
}


/**
  * @brief This inits the dma that moves data from portE to mem buffer
  *        on trigger of the channel 4 port A:11 ccp line
  *         
  * @note   this is the input of data and control signals from the camera
  * @param  none
  * @retval none
  */
void initDMA1(void)
{
	GPIO_InitTypeDef  			GPIO_InitStructure;
	DMA_InitTypeDef				DMA_InitStructure;
	NVIC_InitTypeDef   			NVIC_InitStructure;

	// turn on the clocks for DMA controler(s)
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

	// in case they want to re-enable
	//Disable DMA
	DMA_Cmd(DMA1_Channel4, DISABLE);
	// clear flag
	DMA_ClearFlag(DMA1_FLAG_TC4);
 	
	// camera data & control pins 
	// pins 0-7 are data lines from cam, lineValid 10, Framevalid 11, Pclk 12??
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_All;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStructure);

	// DMA Channel Configuration 
	DMA_InitStructure.DMA_PeripheralBaseAddr 	= (uint32_t)&GPIOE->IDR; 	// we start after a space so we can add in header and send data
	DMA_InitStructure.DMA_MemoryBaseAddr 		= (uint32_t)Data_Buffer;
	DMA_InitStructure.DMA_DIR 					= DMA_DIR_PeripheralSRC;
	DMA_InitStructure.DMA_BufferSize 			= bufferSize;				// subtract out the header 
	DMA_InitStructure.DMA_PeripheralInc 		= DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc 			= DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize 	= DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure.DMA_MemoryDataSize 		= DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_Mode 					= DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority 				= DMA_Priority_VeryHigh;
	DMA_InitStructure.DMA_M2M 					= DMA_M2M_Disable;
	DMA_DeInit(DMA1_Channel4);
	DMA_Init(DMA1_Channel4, &DMA_InitStructure);

	// enable interrupt on transfer complete of dma
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; // ??
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;		 // ??
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	//DMA int on transfer complete, triggers when data has been transfered (buffersize)
	DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
	 
	// keep track of the ticks, current system ticks
	//DMATicks = SysTick->VAL;

	// set this to 0 so we know to recalc it
	//DMAFQ = 0;

	// Enable DMA Channel
	// DMA_Cmd(DMA1_Channel4, ENABLE);

}

/**
  * @brief 
  *       
  *         
  * @note   dmaFQ =  1/(((float)(1/(float)SystemCoreClock)*(float)DMATicks)/bufferSize);
  * @param  none
  * @retval none
  */
void DMA1_Channel4_IRQHandler(void)
{
	// clear TC int here for dma1 channel 4
	DMA_ClearITPendingBit(DMA1_IT_TC4);

	// set a flag so the main loop knows to send valid data

}
/**
  * @brief 	NOT ENABLED ! 
  *       
  *         
  * @note cant handle this and anything else, if its 8clock cycles to get here thats 6MHz*8=48 ? BaD!! 
  * @param  none
  * @retval none
  */
void TIM1_UP_IRQHandler(void)
{
	//TIM_DMACmd(TIM1, TIM_DMA_CC4, DISABLE);
	TIM_Cmd(TIM1, DISABLE);
	// clear the pending bit so we dont block 
	TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
	pwmTicks++;

	
}





/**
  * @brief 
  *       
  *         
  * @note   
  * @param  none
  * @retval none
  */
void initLed(void)
{
	GPIO_InitTypeDef  			GPIO_InitStructure;

	// turn on the love
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
	#ifdef STM32F10X_LD_VL

	// two of the four led's on the board
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_8 | GPIO_Pin_9 ;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	#else // hd board
	// two of the four led's on the board
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_6 | GPIO_Pin_7 ;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE );
	// other two are on port D
	GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_6 | GPIO_Pin_13 ;
	GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed 		= GPIO_Speed_50MHz;
	GPIO_Init(GPIOD, &GPIO_InitStructure);
	#endif

}

// shows value on leds from 1-F
void setLeds(u8 value)
{
//	#ifdef STM32F10X_LD_VL
//	
//	GPIO_ResetBits(GPIOC, GPIO_Pin_8);
//	GPIO_ResetBits(GPIOC, GPIO_Pin_9);
//	switch(value)
//	{
//		case 0x00:
//			break;
//		case 0x01:
//			GPIO_SetBits(GPIOC, GPIO_Pin_8);
//			break;
//		case 0x02:
//			GPIO_SetBits(GPIOC, GPIO_Pin_9);
//			break;
//		case 0x03:
//			GPIO_SetBits(GPIOC, GPIO_Pin_8);
//			GPIO_SetBits(GPIOC, GPIO_Pin_9);
//			break;
//		default:
//			GPIO_SetBits(GPIOC, GPIO_Pin_8);
//			GPIO_SetBits(GPIOC, GPIO_Pin_9);
//			break;  
//	}
//
//	#else // hd board
//
//	GPIO_ResetBits(GPIOC, GPIO_Pin_6);
//	GPIO_ResetBits(GPIOC, GPIO_Pin_7);
//	GPIO_ResetBits(GPIOD, GPIO_Pin_6);
//	GPIO_ResetBits(GPIOD, GPIO_Pin_13);
//
//	// todo define bits here as bit0-3
//	// so we can change easily later
//
//	switch(value)
//	{
//		case 0x00:
//			break;
//		case 0x01:
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			break;
//		case 0x02:
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x03:
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x04:
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			break;
//		case 0x05:
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			break;
//		case 0x06:
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x07:
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x08:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			break;
//		case 0x09:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			break;
//		case 0x0A:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x0B:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x0C:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			break;
//		case 0x0D:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			break;
//		case 0x0E:
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;
//		case 0x0F:
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break; 
//		default:
//			GPIO_SetBits(GPIOD, GPIO_Pin_13);
//			GPIO_SetBits(GPIOD, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_6);
//			GPIO_SetBits(GPIOC, GPIO_Pin_7);
//			break;    
//		}
//		#endif				
}



void USART1_Config(void)
{
	USART_InitTypeDef 		USART_InitStructure;
	
	//Usart2_GPIO();
	GPIO_InitTypeDef GPIO_InitStructure;
	

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	         
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
	GPIO_Init(GPIOA, &GPIO_InitStructure);		   

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	        
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
	GPIO_Init(GPIOA, &GPIO_InitStructure);	
	USART_Cmd(USART1, DISABLE);

    USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);

	// hold onto your butts
	USART_Cmd(USART1, ENABLE);
}


void USART3_Config(void)
{
	USART_InitTypeDef 		USART_InitStructure;
	
	//Usart2_GPIO();
	GPIO_InitTypeDef GPIO_InitStructure;
	

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE); 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	         
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
	GPIO_Init(GPIOB, &GPIO_InitStructure);		   

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	        
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
	GPIO_Init(GPIOB, &GPIO_InitStructure);
		
	USART_Cmd(USART3, DISABLE);

    USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART3, &USART_InitStructure);

	// hold onto your butts
	USART_Cmd(USART3, ENABLE);
}

